用ggplot2制作静态数据可视化报告!
对于可视化呈现来说,一个图表呈现的数据信息是有限的,更多时候,我们需要呈现的是多角度多维度的数据信息。
这时候,就需要用在同一个版面上容纳多副相关的图表形成一个版面的可视化报告,今天是网易数独的一个数据报告,使用ggplot还原了(其中用到的国旗出现了点儿小bug,至今没有解决,有知情的的大神还望赐教!)
library("tidyverse")
library("scales")
library("countrycode")
library("ggimage")
library("grid")
library("Rmisc")
library("showtext")
library("Cairo")
以下是原图:
以下是模仿过程的代码:
很明显这个版面是由上下两幅图表组成的,那个使用R来做也是同样的思路,制作两幅 单独的图然后将二者合并。
图一(上半部分圆环图)
name<-c("司法界","商人","外交领域","军人","记者","经济学家","医学界","学术界","工程师")
label<-factor(name,levels=name,order=T)
percent<-c(0.196,0.166,0.126,0.107,0.083,0.083,0.082,0.078,0.072)
mydata<-data.frame(label,percent)
mydata$anti_percent<-1-mydata$percent
mydata1<-gather(mydata,index,Percent,-label)
conservation_status <-paste0(name,"\n",percent(percent))
names(conservation_status)<-name
global_labeller <-labeller(.defalut=label_value,label=conservation_status)
font.add("myfont","msyhl.ttc")
p1<-ggplot()+
geom_col(data=mydata1,aes(x=1,y=Percent,fill=index),width=.2)+
scale_fill_manual(values=c("percent"="#00A0E9","anti_percent"="#EAEBEB"),guide=FALSE)+
xlim(0.6,1.1)+
coord_polar(theta="y")+
facet_grid(.~label,labeller=global_labeller)+
theme_minimal()+
theme(
line=element_blank(),
axis.text=element_blank(),
title=element_blank(),
panel.spacing=unit(0,"cm"),
strip.text=element_text(family="myfont",size=25,lineheight=1.2),
plot.margin=unit(c(.5,3,0,2),'lines')
);p1
setwd("F:/数据可视化/R/R语言学习笔记/可视化/ggplot2/商务图表")
CairoPNG(file="infography1.png",width=1600,height=480)
showtext.begin()
grid.newpage()
p1
showtext.end()
dev.off()
----------------------------
图二:(堆积条形图)
name2<-c("学术界","商人","公务员","外交领域","经济学家","工程师","司法界","医学界","军人","教育工作者","其他")
巴西<-c(1,1.7,0,0.5,1.4,0.3,2.2,2.25,0.3,1.4)
中国<-c(0.15,0,3.2,0.5,0.35,6.3,0.75,0.2,1.1,1.1)
埃及<-c(3.15,0.5,0.3,1.7,1.9,1.9,3.5,0.4,0.9,0)
印度<-c(0.6,2.1,0.35,0.4,1.9,1,4.2,0.5,0,0)
韩国<-c(2,1.6,2.1,1.7,1.75,0,1.2,0,3.2,0)
美国<-c(0,2.6,0.2,0.1,0,0,8.6,0.4,1,1.1)
colorpalette<-c("#5B8DA2","#155E7B","#E49D85","#1C98CB","#6CC5E8","#22918B","#6DB3A8","#211E1F","#E35840","#76261C","#798A94")
mydata2<-data.frame(巴西,中国,埃及,印度,韩国,美国)
mydata2<-data.frame(t(mydata2))
mydata2[,11]<-16-apply(mydata2,1,sum)
names(mydata2)<-name2
mydata2<-mydata2/16
mydata2$country<-rownames(mydata2)
coname<-c("Brazil","China","Egypt","India","Korea","United States of America")
code=countrycode(coname, "country.name", "iso2c")
mydata2$code<-code
rownames(mydata2)<-NULL
mydata2<-cbind(mydata2[,12:13],mydata2[,1:11])
mydata3<-gather(mydata2,cato,percent,-1:-2)
mydata3$cato<-factor(mydata3$cato,levels=rev(name2),order=T)
p2<-ggplot(data=mydata3,aes(x=country,y=percent,fill=cato))+
geom_bar(stat="identity",width=0.8)+
expand_limits(y =-.1) +
scale_x_discrete(limits=rev(c("巴西","中国","埃及","印度","韩国","美国")))+
scale_fill_manual(values=rev(colorpalette),breaks=rev(levels(mydata3$cato)))+
geom_flag(y=-.08,aes(image=code),size=.1) +
guides(fill=guide_legend(keywidth=3.5,keyheight=.2,nrow=1,title.hjust=.5,title.vjust=.5,title=NULL))+
geom_text(aes(label=ifelse(mydata3$percent!=0,percent(mydata3$percent),"")),position=position_stack(vjust=0.5),family="myfont",size=6.5,col="white")+
coord_flip()+
theme_transparent()+
theme(
text=element_text(family="myfont",size=25,lineheight=1.2),
legend.direction="horizontal",
legend.key.size=unit(.5,'cm'),
legend.position="top",
line=element_blank(),
panel.spacing=unit(1,"cm"),
title=element_blank(),
axis.text.x=element_blank(),
plot.margin=unit(c(0,0,0,2),'lines')
);p2
setwd("F:/数据可视化/R/R语言学习笔记/可视化/ggplot2/商务图表")
CairoPNG(file="infography2.png",width=1600,height=720)
showtext.begin()
grid.newpage()
p2
showtext.end()
dev.off()
三:将两幅图表合并:
setwd("F:/数据可视化/R/R语言学习笔记/可视化/ggplot2/商务图表")
CairoPNG(file="infography.png",width=1600,height=1200)
showtext.begin()
grid.newpage()
pushViewport(viewport(layout=grid.layout(8,5)))
vplayout<-function(x,y){viewport(layout.pos.row =x,layout.pos.col=y)}
print(p1,vp=vplayout(1:3,1:5))
print(p2,vp=vplayout(4:8,1:5))
showtext.end()
dev.off()
虽然R语言中有很成熟的自动化报告输出类的包,但是依靠RMD输出的文件更适合作为多字少图的文档去阅读,而想要呈现或者输出高清晰高分辨率的信息图和仪表盘,依靠Markdown代码确实是捉襟见肘。
所以我还是习惯以固有的模块化思维来考虑问题(这可能是之前学习Exce仪表盘时候留下的痼疾),今天只是一个开始,今后的图表案例都会是稍显综合性的图表版面控制和模块化内容输出。
欢迎关注魔方学院QQ群